package com.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schedule.mapper.ArrangeInformationMapper;
import com.schedule.mapper.EventArrangeMapper;
import com.schedule.pojo.*;
import com.schedule.service.ICompetitionService;
import com.schedule.service.IEventArrangeService;
import com.schedule.service.IYardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author star
 * @since 2022-03-05
 */
@Service
public class EventArrangeServiceImpl extends ServiceImpl<EventArrangeMapper, EventArrange> implements IEventArrangeService {

    @Autowired
    IYardService yardService;
    @Autowired
    ICompetitionService competitionService;
    @Autowired
    ArrangeInformationMapper arrangeInformationMapper;
    @Autowired
    EventArrangeMapper eventArrangeMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 编排算法
     * @param competitionDays
     * @return
     */
    public List<EventArrange> algori(int competitionDays){
        List<EventArrange> eventArranges=new ArrayList<>();
        //场地信息
        List<Yard> yardList = yardService.list();
        //场地数量
        int yardCount=yardList.size();
        //时间-场地表
        int [][] place_time=new int[20][yardCount];
        List<String> timeOfDay=new ArrayList<>();

//        ArrayList<Date> dates=new ArrayList<>();
//        dates.add(new Date(2022,5,13,8,00,00));
//        dates.add(new Date(2022,5,13,8,00,00));
        //初始化
        for(int i=0;i<competitionDays;i++){
            for(int j=0;j<yardCount;j++){
                place_time[i*3][j]=240+20+10;
                place_time[i*3+1][j]=180+20+10;
                place_time[i*3+2][j]=120+20+10;
            }
        }
        timeOfDay.add("8:00");
        timeOfDay.add("14:00");
        timeOfDay.add("19:00");

        //获取比赛信息
        List<arrangeInformation> arrangeInformations = arrangeInformationMapper.getArrangeInformation();
        if(0==arrangeInformations.size()){
            return eventArranges;
        }
        //排序
        Collections.sort(arrangeInformations, new Comparator<arrangeInformation>() {
            @Override
            public int compare(arrangeInformation o1, arrangeInformation o2) {
                return ((o2.getEnrollment()*o2.getEstimatedTime())-(o1.getEnrollment()*o1.getEstimatedTime()));
            }
        });


        int k=0;
        int countFlag=0;
        EventArrange eventArrange=new EventArrange();
        eventArrange.setCName(arrangeInformations.get(k).getCName());
        int len=arrangeInformations.size();
        while(k<len){
            for(int i=0;i<3*competitionDays&&k<len;i++){
                for(int j=0;j<yardCount&&k<len;j++){
                    int temp=arrangeInformations.get(k).getEnrollment()*arrangeInformations.get(k).getEstimatedTime()+20;
                    if(temp<=place_time[i][j]){
                        place_time[i][j]=place_time[i][j]-temp-20;
                        arrangeInformations.get(k).setEnable(true);
                        eventArrange.setEventYardid(yardList.get(j).getId());
                        eventArrange.setEventTime(timeOfDay.get(i%3));
                        eventArrange.setEventDay(i/3+1);
                        eventArranges.add(eventArrange);
                        k++;
                        if(k<len){
                            countFlag=0;
                            eventArrange=new EventArrange();
                            eventArrange.setCName(arrangeInformations.get(k).getCName());
                        }else {
                            break;
                        }
                    }else{
                        countFlag++;
                    }
                }
            }
            if(countFlag>(3*competitionDays*yardCount)){
                break;
            }
        }

        return eventArranges;
    }


    /**
     * 保存编排结果
     * @param eventArranges
     * @return
     */
    @Override
    public void addOrUpdateResult(List<EventArrange> eventArranges) {
        Integer integer = eventArrangeMapper.addOrUpdate(eventArranges);
        System.out.println(integer);
    }

    /**
     * 更新编排结果
     * @param eventArranges
     * @return
     */
    @Override

    public boolean updateArrangeResult(Integer eventId,List<EventArrange> eventArranges) {
        eventArrangeMapper.delete(new QueryWrapper<EventArrange>().eq("eventId", eventId));
//        jdbcTemplate.execute("delete FROM t_event_arrange where eventId = '"+eventId+"' ");
        if(null==eventArranges||0==eventArranges.size()){
            return true;
        }
        Integer result=eventArrangeMapper.insertRecord(eventId,eventArranges);
        if(result==eventArranges.size()){
            return true;
        }
        return false;
    }


}
